package com.net;

import com.lin.entity.Font;
import com.lin.entity.Prop;
import com.lin.entity.Scene;
import com.lin.entity.Waypoint;
import okhttp3.Interceptor;
import org.zjx.net.BaseObserver;
import org.zjx.net.INetworkRequiredInfo;
import org.zjx.net.NetworkApi;

public class Network {

    public Network() {
        NetworkApi.init(new INetworkRequiredInfo() {
            @Override
            public boolean isDebug() {
                return false;
            }

            @Override
            public String getHost() {
                return "http://127.0.0.1:8080";
            }

            @Override
            public String getTestHost() {
                return "";
            }

            @Override
            public Interceptor getMorseUrl() {
                return null;
            }
        });
    }


    public void saveFont(Callback callback, Font font) {
        NetworkApi.createService(ApiService.class)
                .saveFont(font)
                .compose(NetworkApi.applySchedulers(new BaseObserver<Result<String>>() {

                    @Override
                    public void onSuccess(Result<String> key) {
                        if (callback != null) callback.onResult(key.getData());
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        if (callback != null) callback.onError(e.toString());
                    }
                }));
    }

    public void saveProp(Callback callback, Prop prop) {
        NetworkApi.createService(ApiService.class)
                .saveProp(prop)
                .compose(NetworkApi.applySchedulers(new BaseObserver<Result<String>>() {

                    @Override
                    public void onSuccess(Result<String> key) {
                        if (callback != null) callback.onResult(key.getData());
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        if (callback != null) callback.onError(e.toString());
                    }
                }));
    }

    public void saveScreen(Callback callback, Scene scene) {
        NetworkApi.createService(ApiService.class)
                .saveScreen(scene)
                .compose(NetworkApi.applySchedulers(new BaseObserver<Result<String>>() {

                    @Override
                    public void onSuccess(Result<String> key) {
                        if (callback != null) callback.onResult(key.getData());
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        if (callback != null) callback.onError(e.toString());
                    }
                }));
    }

    public void saveWayPoint(Callback callback, Waypoint waypoint) {
        NetworkApi.createService(ApiService.class)
                .saveWaypoint(waypoint)
                .compose(NetworkApi.applySchedulers(new BaseObserver<Result<String>>() {

                    @Override
                    public void onSuccess(Result<String> key) {
                        if (callback != null) callback.onResult(key.getData());
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        if (callback != null) callback.onError(e.toString());
                    }
                }));
    }

    public interface Callback {
        void onResult(Object object);
        void onError(String error);
    }
}
